package com.weilele.leisure.time.ui.dialog


import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.view.WindowManager
import android.widget.FrameLayout
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.GridLayoutManager
import com.weilele.base.library.BaseBindingPopupWindow
import com.weilele.leisure.time.R
import com.weilele.leisure.time.bean.VideoDetailBean
import com.weilele.leisure.time.databinding.PopupWindowSelectListBinding
import com.weilele.leisure.time.ui.holder.PlayListItemHolder
import com.weilele.leisure.time.ui.module.video_detail.VideoDetailViewModel
import com.weilele.mvvm.adapter.refreshList
import com.weilele.mvvm.base.helper.createViewModel
import com.weilele.mvvm.base.livedata.LiveDataWrap
import com.weilele.mvvm.base.livedata.data
import com.weilele.mvvm.base.livedata.wrapSuccessObserver
import com.weilele.mvvm.utils.activity.dp
import com.weilele.mvvm.utils.activity.findFragment
import com.weilele.mvvm.utils.activity.getActivity
import com.weilele.mvvm.utils.android_r.fullScreen
import com.weilele.mvvm.utils.android_r.isPortraitScreen
import com.weilele.mvvm.utils.safeGet

class SelectListPopupWindow : BaseBindingPopupWindow<PopupWindowSelectListBinding>() {
    companion object {
        fun show(view: View): SelectListPopupWindow? {
            val vm =
                view.findFragment<Fragment>()?.parentFragment?.createViewModel<VideoDetailViewModel>()
            if (vm?.playListChangeData?.data?.first.isNullOrBlank()) {
                //没有播放列表不展示
                return null
            }
            return SelectListPopupWindow().apply {
                videoDetailViewModel = vm
                width = WindowManager.LayoutParams.WRAP_CONTENT
                height = WindowManager.LayoutParams.MATCH_PARENT
                showAtLocation(view, Gravity.END, 0, 0)
            }
        }
    }

    private var videoDetailViewModel: VideoDetailViewModel? = null
    private var playersDTO: VideoDetailBean.PlayersDTO? = null
    private val currentPlayList by lazy {
        var currentPlayList: MutableList<VideoDetailBean.PlayersDTO.DatasDTO>? = null
        videoDetailViewModel?.let { vm ->
            val current = vm.playListChangeData.data
            val currentKey = current?.first
            vm.videoDetailData.data?.players?.forEach {
                if (it.key == currentKey) {
                    //找到对应播放列表
                    playersDTO = it
                    currentPlayList = it.datas
                    return@forEach
                }
            }
        }
        currentPlayList
    }
    private val adapter by lazy {
        PlayListItemHolder.newInstance().apply {
            setOnHolderClickListener { holder, clickView ->
                videoDetailViewModel?.let {
                    it.startPlay(
                        it.videoDetailData.data,
                        playersDTO,
                        this.currentList.toMutableList(),
                        holder.bindingPosition
                    )
                }
            }
        }
    }

    override fun initUi(savedInstanceState: Bundle?) {
        super.initUi(savedInstanceState)
        mBinding.root.adapter = adapter
        adapter.refreshList(currentPlayList)
        //滚动到对应位置
        videoDetailViewModel?.playListChangeData?.data?.second?.let {
            mBinding.root.layoutManager.safeGet<GridLayoutManager>()
                ?.scrollToPositionWithOffset(it, 0)
        }
    }

    override fun getObserverLiveDataForever(): List<LiveDataWrap>? {
        val playListChangeData = videoDetailViewModel?.playListChangeData ?: return null
        return listOf(
            playListChangeData wrapSuccessObserver ::onPlayChange
        )
    }

    private fun onPlayChange(pair: Pair<String?, Int>) {
        adapter.notifyDataSetChanged()
    }

    override fun getDialogDimAmount(): Float {
        return 1f
    }

    override fun getDialogAnim(isAsDropDown: Boolean, gravity: Int): Int {
        return R.style.SelectListAnim
    }

    override fun onRootViewLayoutParams(isAsDropDown: Boolean, lp: FrameLayout.LayoutParams) {
        super.onRootViewLayoutParams(isAsDropDown, lp)
        lp.gravity = Gravity.END
        lp.width = 360.dp
        lp.height = FrameLayout.LayoutParams.MATCH_PARENT
    }
}